package com.kitty.game.pet.service;


import com.kitty.common.cache.RedisService;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.difu.domin.*;
import com.kitty.game.difu.message.ReqApplyGuiChongQinMi;
import com.kitty.game.difu.service.handler.GhostNingShen;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.EquipField;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.pet.message.RespPetInfo;
import com.kitty.game.pet.message.vo.PetInfo;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.JsonUtils;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

import static java.lang.Thread.sleep;

@Service
@Slf4j
public class PetNingShenService {
    @Autowired
    protected PetService petService;
    @Autowired
    RoleService roleService;
    @Autowired
    RedisService redisService;
    @Autowired
    EquipService equipService;
    private static List<String> list = Arrays.asList("life_effect","mana_effect","mag_effect","phy_effect","speed_effect");

    public void startNingShen(IoSession session, GhostNingShen ghostNingShen){
        Role role = SessionUtils.getRoleBySession(session);
        int pet_no = ghostNingShen.getPet_no();
        int index = ghostNingShen.getIndex();
        int limit = ghostNingShen.getLimit();
        Pet zhuPet = null;
        Map<Integer, Pet> allPets = role.getPetBox().getAllPets();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if(pet.getPosition() == pet_no) {
                zhuPet = pet;
                break;
            }
        }
        if(zhuPet != null){
            sendNingShenPanel( role, zhuPet,  ghostNingShen);
            equipService.delRoloEquip("凝神草", limit, true, role);
            petService.refreshPetInfo(role, zhuPet);
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"201019");
        }

    }




    //查询培元加成信息
    public  void getNingShenPanel(Role role,Pet pet){
        RespPetInfo respPetInfo = new RespPetInfo();
        List<PetInfo> petInfos = new ArrayList<>();
        PetInfo petInfo = new PetInfo();
        petInfo.setPetId(pet.getId());
        petInfo.setPosition(pet.getPosition());
        NingShenFieldOne ningShenField = new NingShenFieldOne();
        getNingShengInfo(pet,ningShenField);
        List<EquipField> petFields = new ArrayList<>();
        petFields.add(ningShenField);
        petInfo.setList(petFields);
        petInfos.add(petInfo);
        respPetInfo.setPetInfos(petInfos);
        MessagePusher.pushMessage(role, respPetInfo);

    }

    public  void getNingShengInfo(Pet zhuPet, NingShenFieldOne basicField){
        List<NingshenFieldAdd> ningshenFieldAddList = zhuPet.getNingshenFieldAdds();
        basicField.setNingShenFieldList(ningshenFieldAddList);
    }


    //发送培元加成面板包
    public  void sendNingShenPanel(Role role,Pet pet, GhostNingShen ghostNingShen){
        RespPetInfo respPetInfo = new RespPetInfo();
        List<PetInfo> petInfos = new ArrayList<>();
        PetInfo petInfo = new PetInfo();
        petInfo.setPetId(pet.getId());
        petInfo.setPosition(pet.getPosition());
        NingShenFieldOne ningShenField = new NingShenFieldOne();
        sendNingShengInfo(pet,ningShenField,  ghostNingShen);
        List<EquipField> petFields = new ArrayList<>();
        petFields.add(ningShenField);
        petInfo.setList(petFields);
        petInfos.add(petInfo);
        respPetInfo.setPetInfos(petInfos);
        MessagePusher.pushMessage(role, respPetInfo);

    }
    //发送培元加成信息
    public  void sendNingShengInfo(Pet zhuPet, NingShenFieldOne basicField, GhostNingShen ghostNingShen){
        List<NingshenFieldAdd> ningShenFieldList = new ArrayList<>();
        List<NingshenFieldAdd> ningshenFieldAddList = zhuPet.getNingshenFieldAdds();
        int ranm = ThreadLocalRandom.current().nextInt(3)+1;


        int index = ghostNingShen.getIndex();
        for (NingshenFieldAdd fieldAdd : ningshenFieldAddList) {
            if(index == fieldAdd.getIndex()){
                ningshenFieldAddList.remove(fieldAdd);
                break;
            }
        }
        NingshenFieldAdd ningshenFieldAdd  = new NingshenFieldAdd();
        ningshenFieldAdd.setIndex((byte)index);
        List<NingshenFieldList> ningshenFieldListList = new ArrayList<>();
        for (int j = 0; j < ranm; j++) {
            int ningshenMaxValue = getNingshenMaxValue(index);
            int ningShenValue = ThreadLocalRandom.current().nextInt(ningshenMaxValue)+1;
            int ningShenKey = ThreadLocalRandom.current().nextInt(5);
            NingshenFieldList ningshenFieldList = new NingshenFieldList();
            ningshenFieldList.setKey(list.get(ningShenKey));
            ningshenFieldList.setMaxValue(ningshenMaxValue);
            ningshenFieldList.setValue(ningShenValue);
            ningshenFieldListList.add(ningshenFieldList);
        }
        ningshenFieldAdd.setNingshenFieldListList(ningshenFieldListList);
        ningshenFieldAddList.add(ningshenFieldAdd);
        basicField.setNingShenFieldList(ningshenFieldAddList);
    }
    public  int  getNingshenMaxValue(int index){
        String key = "ningshen_"+index;
        String magnification = DataCache.REDIS_DATA.get(key).toString();
        return Integer.parseInt(magnification);
    }
    public static void main(String[] args) {
//        PetYuanCurStateInfoBuilder petYuanCurStateInfoBuilder = PetYuanCurStateInfoBuilder.builder() ;
//        Object o = JSON.toJSON(petYuanCurStateInfoBuilder);
//        System.out.println(o.toString());
//        System.out.println( String.valueOf(petYuanCurStateInfoBuilder));
        System.out.println((int)1/2);
        System.out.println((int)1/2);
        System.out.println((int)(5/2));
        System.out.println((int)(7/2));
        System.out.println((int)(9/2));
//    Integer a= new Integer(1);
//    Integer b =new Integer(1);
//    System.out.println(a==b);
//    System.out.println(10>>1);
    }
    public void applyGuiChongQinMi(IoSession session, ReqApplyGuiChongQinMi reqApplyGuiChongQinMi){
        if (reqApplyGuiChongQinMi.getAmount() < 0) {
            return;
        }
        String SUCCESS_MESSAGE = "#R{0}#n成功增加#R{1}#n点亲密度。";
        Role role = SessionUtils.getRoleBySession(session);
        EquipService equipService = SpringUtils.getBean(EquipService.class);
        PetService petService = SpringUtils.getBean(PetService.class);
        Pet pet = petService.getPetByPos((byte)reqApplyGuiChongQinMi.getNo(), role);
        if (pet == null) return;
        log.info("亲密喂养=={}", pet.getNickname());
        int itemNum = equipService.getCount("鬼宠亲密丹", reqApplyGuiChongQinMi.isLimit(), role);
        if (itemNum < reqApplyGuiChongQinMi.getAmount()) {
            MessagePusher.pushMessage(session, new RespMsg("你的鬼宠亲密丹不足。"));
            return;
        }
        int addIntimacy = 2000 * reqApplyGuiChongQinMi.getAmount();
        int limitItemNum = 0;
        limitItemNum += equipService.delRoloEquip("鬼宠亲密丹", reqApplyGuiChongQinMi.getAmount(), reqApplyGuiChongQinMi.isLimit(), role);

        if (limitItemNum >= 1) {
            petService.addLimitTime(pet, limitItemNum);
        }
        pet.setIntimacy(pet.getIntimacy() + addIntimacy);
        role.save();
        //面板属性
        petService.refreshPetInfo(role,pet);
        RespUpdate respUpdate = new RespUpdate();
        List<FiedValue> list = new ArrayList<>();
        list.add(new FiedValue(61, pet.getLongevity()));
        list.add(new FiedValue(923, pet.getIntimacy()));
        list.add(new FiedValue(63, pet.getIntimacy()));
        list.add(new FiedValue(270, pet.getLimitTime()));
        respUpdate.setRoleId(pet.getId());
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, respUpdate);
        MessagePusher.pushMessage(role, new RespNotifyMiscEx(MessageFormat.format(SUCCESS_MESSAGE, pet.getName(), String.valueOf(addIntimacy))));
    }

}
